package com.inspur.attendance.Service.ServiceImpl;

import com.inspur.attendance.Dao.DepartmentDao;
import com.inspur.attendance.Dao.UserDao;
import com.inspur.attendance.Entity.Department;
import com.inspur.attendance.Entity.User;
import com.inspur.attendance.Service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    DepartmentDao departmentDao;
    @Autowired
    UserDao userDao;

    @Override
    public Page<Department> getAllDepartment(int pageNo, int pageSize) {
        System.out.println(pageNo);
        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, Sort.by("departmentId").descending());
        return departmentDao.findAll(pageable);
    }

    @Transactional
    @Modifying
    @Override
    public void addDepartment(Department department) {
        departmentDao.save(department);
    }

    @Override
    public void delDepartment(Long id) {
        departmentDao.deleteByDepartmentId(id);
        userDao.delDepartmentName(String.valueOf(id));
    }

    @Override
    public Department findById(Long departmentId) {
        return departmentDao.findTopByDepartmentId(departmentId);
    }
}
